package com.demomaster.quickaccessibility.service;

import android.view.accessibility.AccessibilityEvent;

import com.demomaster.quickaccessibility.interfaces.OnAccessibilityEventListener;

public class EventFilter {
    private String action;
    private OnAccessibilityEventListener listener;
    private int eventTypes;
    private int frequencyMillis;
    private String packageName;
    private long lastEventTime;

    public EventFilter(EventFilter filter, OnAccessibilityEventListener listener) {
        this.listener = listener;
        this.eventTypes = filter.eventTypes;
        this.frequencyMillis = filter.frequencyMillis;
        this.packageName = filter.packageName;
        this.lastEventTime = filter.lastEventTime;
    }

    public EventFilter(String action) {
        this.action = action;
    }

    public boolean filterEvent(AccessibilityEvent event) {
        long currentTime = System.currentTimeMillis();
        if ((event.getEventType() & eventTypes) == 0) {
            return false; // Event type does not match
        }
        if (packageName != null && !packageName.equals(event.getPackageName().toString())) {
            return false; // Package name does not match
        }
        if (currentTime - lastEventTime < frequencyMillis) {
            return false; // Frequency limit exceeded
        }
        lastEventTime = currentTime;
        return true;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public void setListener(OnAccessibilityEventListener listener) {
        this.listener = listener;
    }

    public int getEventTypes() {
        return eventTypes;
    }

    public void setEventTypes(int eventTypes) {
        this.eventTypes = eventTypes;
    }

    public int getFrequencyMillis() {
        return frequencyMillis;
    }

    public void setFrequencyMillis(int frequencyMillis) {
        this.frequencyMillis = frequencyMillis;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public long getLastEventTime() {
        return lastEventTime;
    }

    public void setLastEventTime(long lastEventTime) {
        this.lastEventTime = lastEventTime;
    }

    public OnAccessibilityEventListener getListener() {
        return listener;
    }
}
